package com.jspgou.cms.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.springframework.stereotype.Repository;

import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;
import com.jspgou.cms.dao.RoleDao;
import com.jspgou.cms.entity.Role;

@Repository
public class RoleDaoImpl extends HibernateBaseDao<Role, Long> implements RoleDao {
	public Pagination getPage(int pageNo, int pageSize) {
		Criteria crit = createCriteria();
		Pagination page = findByCriteria(crit, pageNo, pageSize);
		return page;
	}
	
	public Pagination getPage(Long websiteId,Long storeId,int pageNo, int pageSize){
		Finder f=Finder.create("from Role bean where 1=1");
		if(websiteId!=null){
			f.append(" and bean.website.id=:websiteId");
			f.setParam("websiteId", websiteId);
		}
		if(storeId!=null){
			f.append(" and bean.store.id=:storeId");
			f.setParam("storeId", storeId);
		}
		return find(f,pageNo,pageSize);
	}
	
	@SuppressWarnings("unchecked")
	public List<Role> getList(Long websiteId,Long storeId){
		Finder f=Finder.create("from Role bean where 1=1");
		if(websiteId!=null){
			f.append(" and bean.website.id=:websiteId");
			f.setParam("websiteId", websiteId);
		}
		if(storeId!=null){
			f.append(" and bean.store.id=:storeId");
			f.setParam("storeId", storeId);
		}
		return find(f);
	}

	public Role findById(Long id) {
		Role entity = get(id);
		return entity;
	}

	public Role save(Role bean) {
		getSession().save(bean);
		return bean;
	}

	public Role deleteById(Long id) {
		Role entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	public Role update(Role bean){
		getSession().merge(bean);
		//getSession().saveOrUpdate(bean);
		return bean;
	}
	@Override
	protected Class<Role> getEntityClass() {
		return Role.class;
	}
}